package GUILayer;

import ControlLayer.LeaseCtr;

import ModelLayer.Lease;
import ModelLayer.LeaseItemUnit;
import ModelLayer.LeaseRecords;
import ModelLayer.RealException;
import ModelLayer.Status;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;

public class BorrowedLeaseUI extends JPanel {

	private static final long serialVersionUID = 1L;

	private LeaseCtr leaseCtr;
	private LeaseRecords leaseRecords;

	private JTextField showEmployeeField;
	private JTextField showIdField;
	private JTextField showCustomerField;
	private JTextField showStatusField;
	private JTextField showPaymentTermField;
	private JTextField showEndDateField;
	private JTextField showStartDateField;

	private JTextField editEmployeeField;
	private JTextField editIdField;
	private JTextField editCustomerField;
	private JTextField editStatusField;
	private JTextField editPaymentTermField;

	private JPanel editLease;

	private DefaultTableModel tableModel;
	private DefaultTableModel tableModel2;
	private DefaultTableModel tableModel3;
	private DefaultTableModel tableModel4;
	private JTable table;
	private JTable table2;
	private JTable table3;
	private JTable table4;

	private JPanel showLease;
	private JPanel showAllLeases;
	private JTextField showAllIdField;

	private JTextField createStartDateField;
	private JTextField createCustomerField;
	private JTextField createEndDateField;
	private JTextField createDeliveryAddressField;

	private JPanel createLease;

	private JPanel leaseContainer;

	private JTextField editStartDateField;

	private JTextField editEndDateField;

	private JTextField editDeliveryAddressField;

	private JRadioButton rdbtnYes;

	private JRadioButton rdbtnNo;

	private JRadioButton rdbtnYes_1;

	private JRadioButton rdbtnNo_1;

	private JTextField showDeliveryAddressField;

	private JRadioButton rdbtnYes_2;

	private JRadioButton rdbtnNo_2;

	public BorrowedLeaseUI() {
		leaseCtr = new LeaseCtr();
		leaseRecords = LeaseRecords.getInstance();
		initialize();
	}

	public void initialize() {

		String loggedInPosition = LoginUI.getLoggedInPosition();

		tableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel.addColumn("ID");
		tableModel.addColumn("Customer");
		tableModel.addColumn("Group");
		tableModel.addColumn("Start Date");
		tableModel.addColumn("End Date");
		tableModel.addColumn("Price");

		tableModel2 = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel2.addColumn("ID");
		tableModel2.addColumn("Name");
		tableModel2.addColumn("Location");
		tableModel2.addColumn("Serial No");
		tableModel2.addColumn("Status");

		tableModel3 = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel3.addColumn("ID");
		tableModel3.addColumn("Name");
		tableModel3.addColumn("Location");
		tableModel3.addColumn("Serial No");
		tableModel3.addColumn("Status");

		tableModel4 = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel4.addColumn("ID");
		tableModel4.addColumn("Name");
		tableModel4.addColumn("Location");
		tableModel4.addColumn("Serial No");
		tableModel4.addColumn("Status");

		this.setLayout(new BorderLayout(0, 0));

		JPanel showAllMenu = new JPanel();
		this.add(showAllMenu, BorderLayout.NORTH);
		showAllMenu.setLayout(new BorderLayout(0, 0));
		showAllMenu.setBorder(BorderFactory.createTitledBorder("Lease Menu"));

		JPanel showAllMenuLeft = new JPanel();
		showAllMenu.add(showAllMenuLeft, BorderLayout.WEST);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("salesman")) {
			JButton btnCreateNewLease = new JButton("Create new lease");
			btnCreateNewLease.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CardLayout cl = (CardLayout) leaseContainer.getLayout();
					cl.show(leaseContainer, "createLease");
					createStartDateField.setText(getCurrentDateAsString());
					createDeliveryAddressField.setEditable(false);
					rdbtnYes_1.setSelected(true);
				}
			});
			showAllMenuLeft.add(btnCreateNewLease);
		}
		JPanel showAllMenuRight = new JPanel();
		showAllMenu.add(showAllMenuRight, BorderLayout.EAST);

		JLabel showAllIdLbl = new JLabel("Id:");
		showAllMenuRight.add(showAllIdLbl);

		showAllIdField = new JTextField();
		showAllMenuRight.add(showAllIdField);
		showAllIdField.setColumns(5);

		JButton showAllBtnSearch = new JButton("Search");
		showAllBtnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showLease(Integer.parseInt(showAllIdField.getText()));
				showAllIdField.setText("");
			}
		});
		showAllMenuRight.add(showAllBtnSearch);

		leaseContainer = new JPanel();
		this.add(leaseContainer);
		leaseContainer.setLayout(new CardLayout(0, 0));

		showLease = new JPanel();
		leaseContainer.add(showLease, "showLease");

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("salesman")) {

			editLease = new JPanel();
			leaseContainer.add(editLease, "editLease");
			editLease.setLayout(new GridLayout(0, 2, 0, 0));

			JPanel editLeaseInfoContainer = new JPanel();
			editLease.add(editLeaseInfoContainer);
			editLeaseInfoContainer.setLayout(new BoxLayout(
					editLeaseInfoContainer, BoxLayout.Y_AXIS));

			JPanel editLeaseInfoPanel = new JPanel();
			editLeaseInfoContainer.add(editLeaseInfoPanel);
			editLeaseInfoPanel.setBorder(BorderFactory
					.createTitledBorder("Edit Lease"));
			editLeaseInfoPanel.setLayout(new BoxLayout(editLeaseInfoPanel,
					BoxLayout.X_AXIS));

			JPanel editLeaseInfo = new JPanel();
			editLeaseInfo.setLayout(new BoxLayout(editLeaseInfo,
					BoxLayout.X_AXIS));
			editLeaseInfoPanel.add(editLeaseInfo);

			JPanel editLeaseLeft = new JPanel();
			editLeaseInfo.add(editLeaseLeft);
			editLeaseLeft.setLayout(new GridLayout(0, 1, 0, 0));

			JPanel editIdLabelPanel = new JPanel();
			editIdLabelPanel.setLayout(new BoxLayout(editIdLabelPanel,
					BoxLayout.X_AXIS));
			editLeaseLeft.add(editIdLabelPanel);

			JLabel editIdLabel = new JLabel("ID");
			editIdLabelPanel.add(editIdLabel);
			editIdLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editCustomerLabelPanel = new JPanel();
			editCustomerLabelPanel.setLayout(new BoxLayout(
					editCustomerLabelPanel, BoxLayout.X_AXIS));
			editLeaseLeft.add(editCustomerLabelPanel);

			JLabel editCustomerLabel = new JLabel("Customer");
			editCustomerLabelPanel.add(editCustomerLabel);
			editCustomerLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editEmployeeLabelPanel = new JPanel();
			editEmployeeLabelPanel.setLayout(new BoxLayout(
					editEmployeeLabelPanel, BoxLayout.X_AXIS));
			editLeaseLeft.add(editEmployeeLabelPanel);

			JLabel editEmployeeLabel = new JLabel("Employee");
			editEmployeeLabelPanel.add(editEmployeeLabel);
			editEmployeeLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editStatusLabelPanel = new JPanel();
			editStatusLabelPanel.setLayout(new BoxLayout(editStatusLabelPanel,
					BoxLayout.X_AXIS));
			editLeaseLeft.add(editStatusLabelPanel);

			JLabel editStatusLabel = new JLabel("Status");
			editStatusLabelPanel.add(editStatusLabel);
			editStatusLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editPaymentTermLabelPanel = new JPanel();
			editPaymentTermLabelPanel.setLayout(new BoxLayout(
					editPaymentTermLabelPanel, BoxLayout.X_AXIS));
			editLeaseLeft.add(editPaymentTermLabelPanel);

			JLabel editPaymentTermLabel = new JLabel("Payment Term");
			editPaymentTermLabelPanel.add(editPaymentTermLabel);
			editPaymentTermLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editStartDateLabelPanel = new JPanel();
			editStartDateLabelPanel.setLayout(new BoxLayout(
					editStartDateLabelPanel, BoxLayout.X_AXIS));
			editLeaseLeft.add(editStartDateLabelPanel);

			JLabel editStartDateLabel = new JLabel("Start Date");
			editStartDateLabelPanel.add(editStartDateLabel);
			editStartDateLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editEndDateLabelPanel = new JPanel();
			editEndDateLabelPanel.setLayout(new BoxLayout(
					editEndDateLabelPanel, BoxLayout.X_AXIS));
			editLeaseLeft.add(editEndDateLabelPanel);

			JLabel editEndDateLabel = new JLabel("End Date");
			editEndDateLabelPanel.add(editEndDateLabel);
			editEndDateLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editDeliveryAddressLabelPanel = new JPanel();
			editDeliveryAddressLabelPanel.setLayout(new BoxLayout(
					editDeliveryAddressLabelPanel, BoxLayout.X_AXIS));
			editLeaseLeft.add(editDeliveryAddressLabelPanel);

			JLabel editDeliveryAddressLabel = new JLabel("Delivery Address");
			editDeliveryAddressLabelPanel.add(editDeliveryAddressLabel);
			editDeliveryAddressLabel
					.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editPickupLabelPanel = new JPanel();
			editPickupLabelPanel.setLayout(new BoxLayout(editPickupLabelPanel,
					BoxLayout.X_AXIS));
			editLeaseLeft.add(editPickupLabelPanel);

			JLabel editPickupLabel = new JLabel("Self Pickup");
			editPickupLabelPanel.add(editPickupLabel);
			editPickupLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel editLeaseRight = new JPanel();
			editLeaseRight.setLayout(new BoxLayout(editLeaseRight,
					BoxLayout.Y_AXIS));
			editLeaseInfo.add(editLeaseRight);

			JPanel editIdFieldPanel = new JPanel(
					new FlowLayout(FlowLayout.LEFT));
			editLeaseRight.add(editIdFieldPanel);

			editIdField = new JTextField();
			editIdField.setEditable(false);
			editIdFieldPanel.add(editIdField);
			editIdField.setHorizontalAlignment(SwingConstants.CENTER);
			editIdField.setColumns(5);

			JPanel editCustomerFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editCustomerFieldPanel);

			editCustomerField = new JTextField();
			editCustomerFieldPanel.add(editCustomerField);
			editCustomerField.setHorizontalAlignment(SwingConstants.CENTER);
			editCustomerField.setColumns(5);

			JPanel editEmployeeFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editEmployeeFieldPanel);

			editEmployeeField = new JTextField();
			editEmployeeFieldPanel.add(editEmployeeField);
			editEmployeeField.setHorizontalAlignment(SwingConstants.CENTER);
			editEmployeeField.setColumns(5);

			JPanel editStatusFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editStatusFieldPanel);

			editStatusField = new JTextField();
			editStatusField.setEditable(false);
			editStatusFieldPanel.add(editStatusField);
			editStatusField.setHorizontalAlignment(SwingConstants.CENTER);
			editStatusField.setColumns(20);

			JPanel editPaymentTermFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editPaymentTermFieldPanel);

			editPaymentTermField = new JTextField();
			editPaymentTermField.setEditable(false);
			editPaymentTermFieldPanel.add(editPaymentTermField);
			editPaymentTermField.setHorizontalAlignment(SwingConstants.CENTER);
			editPaymentTermField.setColumns(5);

			JPanel editStartDateFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editStartDateFieldPanel);

			editStartDateField = new JTextField();
			editStartDateFieldPanel.add(editStartDateField);
			editStartDateField.setHorizontalAlignment(SwingConstants.CENTER);
			editStartDateField.setColumns(15);

			JPanel editEndDateFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editEndDateFieldPanel);

			editEndDateField = new JTextField();
			editEndDateFieldPanel.add(editEndDateField);
			editEndDateField.setHorizontalAlignment(SwingConstants.CENTER);
			editEndDateField.setColumns(15);

			JPanel editDeliveryAddressFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editDeliveryAddressFieldPanel);

			editDeliveryAddressField = new JTextField();
			editDeliveryAddressFieldPanel.add(editDeliveryAddressField);
			editDeliveryAddressField
					.setHorizontalAlignment(SwingConstants.CENTER);
			editDeliveryAddressField.setColumns(25);

			JPanel editPickupFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			editLeaseRight.add(editPickupFieldPanel);

			rdbtnYes = new JRadioButton("Yes");
			rdbtnYes.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					rdbtnYes.setSelected(true);
					rdbtnNo.setSelected(false);
					editDeliveryAddressField.setEditable(false);
				}
			});
			editPickupFieldPanel.add(rdbtnYes);

			rdbtnNo = new JRadioButton("No");
			rdbtnNo.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					rdbtnNo.setSelected(true);
					rdbtnYes.setSelected(false);
					editDeliveryAddressField.setEditable(true);
				}
			});
			editPickupFieldPanel.add(rdbtnNo);

			JPanel editButtonsPanel = new JPanel();
			editLeaseInfoContainer.add(editButtonsPanel);
			editButtonsPanel.setLayout(new BorderLayout(0, 0));

			JPanel editCenterButtonsPanel = new JPanel();
			editButtonsPanel.add(editCenterButtonsPanel, BorderLayout.CENTER);

			JButton editBtnEdit = new JButton("Edit");
			editBtnEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int reply = JOptionPane.showConfirmDialog(null,
							"Are you sure you want to edit this lease ?",
							"Edit Lease", JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							leaseCtr.updateLease(
									Integer.parseInt(editIdField.getText()), 2,
									editCustomerField.getText());
							leaseCtr.updateLease(
									Integer.parseInt(editIdField.getText()), 1,
									editEmployeeField.getText());
							leaseCtr.updateLease(
									Integer.parseInt(editIdField.getText()),
									3,
									String.valueOf(stringToLongDate(editStartDateField
											.getText())));
							leaseCtr.updateLease(Integer.parseInt(editIdField
									.getText()), 4, String
									.valueOf(stringToLongDate(editEndDateField
											.getText())));
							if (rdbtnYes.isSelected()) {
								leaseCtr.noPickup(
										Integer.parseInt(editIdField.getText()),
										"");
								leaseCtr.hasPickup(Integer.parseInt(editIdField
										.getText()));
							} else {
								leaseCtr.noPickup(
										Integer.parseInt(editIdField.getText()),
										editDeliveryAddressField.getText());
							}
						} catch (NumberFormatException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						} catch (RealException e1) {
							success = false;
							JOptionPane.showMessageDialog(null, e1.getError(),
									"Error", JOptionPane.ERROR_MESSAGE);
						} catch (ParseException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						}
						if (success) {
							JOptionPane.showMessageDialog(null,
									"The lease was edited", "Edit Lease",
									JOptionPane.INFORMATION_MESSAGE);
							showLease(Integer.parseInt(editIdField.getText()));
						}
					}
				}
			});
			editCenterButtonsPanel.add(editBtnEdit);

			JButton editBtnCancel = new JButton("Cancel");
			editBtnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					showLease(Integer.parseInt(editIdField.getText()));
				}
			});
			editCenterButtonsPanel.add(editBtnCancel);

			JButton editBtnReturn = new JButton("Return lease");
			editBtnReturn.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean success = true;
					try {
						leaseCtr.returnLease(
								Integer.parseInt(editIdField.getText()),
								String.valueOf(getCurrentDate()));
					} catch (NumberFormatException e1) {
						success = false;
						e1.printStackTrace();
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane
								.showMessageDialog(null,
										"The lease was marked as returned",
										"Return Lease",
										JOptionPane.INFORMATION_MESSAGE);
						showAllLeases();
					}
				}
			});
			editCenterButtonsPanel.add(editBtnReturn);

			JPanel editLeaseItems = new JPanel();
			editLease.add(editLeaseItems);

			JScrollPane scrollPane_2 = new JScrollPane();
			editLeaseItems.add(scrollPane_2);

			table3 = new JTable(tableModel3);

			table3.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
			table3.setAutoCreateRowSorter(true);

			scrollPane_2.setViewportView(table3);

			JButton btnAddLeaseItem = new JButton("Add");
			btnAddLeaseItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					new AddLeaseItemUnitPopupWindow(Integer
							.parseInt(editIdField.getText()));
				}
			});
			editLeaseItems.add(btnAddLeaseItem);

			editLeaseItems.setBorder(BorderFactory
					.createTitledBorder("Lease Items"));

			JButton btnRemoveLeaseItem = new JButton("Remove");
			btnRemoveLeaseItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					new RemoveLeaseItemUnitPopupWindow(Integer
							.parseInt(editIdField.getText()));
				}
			});
			editLeaseItems.add(btnRemoveLeaseItem);

			createLease = new JPanel();
			leaseContainer.add(createLease, "createLease");
			createLease.setLayout(new GridLayout(0, 2, 0, 0));

			JPanel createLeaseInfoContainer = new JPanel();
			createLease.add(createLeaseInfoContainer);
			createLeaseInfoContainer.setLayout(new BoxLayout(
					createLeaseInfoContainer, BoxLayout.Y_AXIS));

			JPanel createLeaseInfoPanel = new JPanel();
			createLeaseInfoContainer.add(createLeaseInfoPanel);
			createLeaseInfoPanel.setBorder(BorderFactory
					.createTitledBorder("Create lease"));
			createLeaseInfoPanel.setLayout(new BoxLayout(createLeaseInfoPanel,
					BoxLayout.X_AXIS));

			JPanel createLeaseInfo = new JPanel();
			createLeaseInfo.setLayout(new BoxLayout(createLeaseInfo,
					BoxLayout.X_AXIS));
			createLeaseInfoPanel.add(createLeaseInfo);

			JPanel createLeaseLeft = new JPanel();
			createLeaseInfo.add(createLeaseLeft);
			createLeaseLeft.setLayout(new GridLayout(0, 1, 0, 0));

			JPanel createCustomerLabelPanel = new JPanel();
			createCustomerLabelPanel.setLayout(new BoxLayout(
					createCustomerLabelPanel, BoxLayout.X_AXIS));
			createLeaseLeft.add(createCustomerLabelPanel);

			JLabel createCustomerLabel = new JLabel("Customer");
			createCustomerLabelPanel.add(createCustomerLabel);
			createCustomerLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createStartDateLabelPanel = new JPanel();
			createStartDateLabelPanel.setLayout(new BoxLayout(
					createStartDateLabelPanel, BoxLayout.X_AXIS));
			createLeaseLeft.add(createStartDateLabelPanel);

			JLabel createStartDateLabel = new JLabel("Start Date");
			createStartDateLabelPanel.add(createStartDateLabel);
			createStartDateLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createEndDateLabelPanel = new JPanel();
			createEndDateLabelPanel.setLayout(new BoxLayout(
					createEndDateLabelPanel, BoxLayout.X_AXIS));
			createLeaseLeft.add(createEndDateLabelPanel);

			JLabel createEndDateLabel = new JLabel("End Date");
			createEndDateLabelPanel.add(createEndDateLabel);
			createEndDateLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createDeliveryAddressLabelPanel = new JPanel();
			createDeliveryAddressLabelPanel.setLayout(new BoxLayout(
					createDeliveryAddressLabelPanel, BoxLayout.X_AXIS));
			createLeaseLeft.add(createDeliveryAddressLabelPanel);

			JLabel createDeliveryAddressLabel = new JLabel("Delivery Address");
			createDeliveryAddressLabelPanel.add(createDeliveryAddressLabel);
			createDeliveryAddressLabel
					.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createPickupLabelPanel = new JPanel();
			createPickupLabelPanel.setLayout(new BoxLayout(
					createPickupLabelPanel, BoxLayout.X_AXIS));
			createLeaseLeft.add(createPickupLabelPanel);

			JLabel createPickupLabel = new JLabel("Self Pickup");
			createPickupLabelPanel.add(createPickupLabel);
			createPickupLabel.setHorizontalAlignment(SwingConstants.CENTER);

			JPanel createLeaseRight = new JPanel();
			createLeaseRight.setLayout(new BoxLayout(createLeaseRight,
					BoxLayout.Y_AXIS));
			createLeaseInfo.add(createLeaseRight);

			JPanel createCustomerFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseRight.add(createCustomerFieldPanel);

			createCustomerField = new JTextField();
			createCustomerFieldPanel.add(createCustomerField);
			createCustomerField.setHorizontalAlignment(SwingConstants.CENTER);
			createCustomerField.setColumns(5);

			JPanel createAddressFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseRight.add(createAddressFieldPanel);

			createStartDateField = new JTextField();
			createStartDateField.setEditable(false);
			createAddressFieldPanel.add(createStartDateField);
			createStartDateField.setHorizontalAlignment(SwingConstants.CENTER);
			createStartDateField.setColumns(15);

			JPanel createPhoneFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseRight.add(createPhoneFieldPanel);

			createEndDateField = new JTextField();
			createPhoneFieldPanel.add(createEndDateField);
			createEndDateField.setHorizontalAlignment(SwingConstants.CENTER);
			createEndDateField.setColumns(15);

			JPanel createGroupFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseRight.add(createGroupFieldPanel);

			createDeliveryAddressField = new JTextField();
			createGroupFieldPanel.add(createDeliveryAddressField);
			createDeliveryAddressField
					.setHorizontalAlignment(SwingConstants.CENTER);
			createDeliveryAddressField.setColumns(25);

			JPanel createPickupFieldPanel = new JPanel(new FlowLayout(
					FlowLayout.LEFT));
			createLeaseRight.add(createPickupFieldPanel);

			rdbtnYes_1 = new JRadioButton("Yes");
			rdbtnYes_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					rdbtnYes_1.setSelected(true);
					rdbtnNo_1.setSelected(false);
					createDeliveryAddressField.setEditable(false);
				}
			});
			createPickupFieldPanel.add(rdbtnYes_1);

			rdbtnNo_1 = new JRadioButton("No");
			rdbtnNo_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					rdbtnYes_1.setSelected(false);
					rdbtnNo_1.setSelected(true);
					createDeliveryAddressField.setEditable(true);
				}
			});
			createPickupFieldPanel.add(rdbtnNo_1);

			JPanel createBottomMenu = new JPanel();
			createLeaseInfoContainer.add(createBottomMenu);
			createBottomMenu.setLayout(new BorderLayout(0, 0));

			JPanel panel = new JPanel();
			createBottomMenu.add(panel, BorderLayout.NORTH);

			JButton createBtnCreate = new JButton("Create");
			panel.add(createBtnCreate);

			JButton createBtnCancel = new JButton("Cancel");
			panel.add(createBtnCancel);
			createBtnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					showAllLeases();
				}
			});
			createBtnCreate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int leaseId = 0;
					boolean success = true;
					try {
						leaseId = leaseCtr.createLease(
								Integer.parseInt(createCustomerField.getText()),
								LoginUI.getLoggedInId(),
								Long.parseLong(String
										.valueOf(stringToLongDate(createStartDateField
												.getText()))),
								Long.parseLong(String
										.valueOf(stringToLongDate(createEndDateField
												.getText()))));
						if (rdbtnYes_1.isSelected()) {
							leaseCtr.hasPickup(leaseId);
						} else {
							leaseCtr.noPickup(leaseId,
									createDeliveryAddressField.getText());
						}
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (ParseException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The lease was created", "Create Lease",
								JOptionPane.INFORMATION_MESSAGE);
						editLease(leaseId);
					}
				}
			});

			JPanel createLeaseItems = new JPanel();
			createLeaseItems.setBorder(BorderFactory
					.createTitledBorder("Lease Items"));
			createLease.add(createLeaseItems);

			JScrollPane scrollPane_3 = new JScrollPane();
			createLeaseItems.add(scrollPane_3);

			table4 = new JTable(tableModel4);

			table4.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
			table4.setAutoCreateRowSorter(true);

			scrollPane_3.setViewportView(table4);

		}

		showAllLeases = new JPanel();
		leaseContainer.add(showAllLeases, "showAllLeases");
		showAllLeases.setLayout(new BorderLayout(0, 0));

		JPanel showAllLeasesPanel = new JPanel();
		showAllLeasesPanel.setBorder(BorderFactory
				.createTitledBorder("All Borrowed Leases"));
		showAllLeases.add(showAllLeasesPanel, BorderLayout.CENTER);

		table = new JTable(tableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

		table.setAutoCreateRowSorter(true);

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int row = table.rowAtPoint(e.getPoint());
				if (e.getClickCount() == 2) {
					showLease(Integer.valueOf((Integer) tableModel.getValueAt(
							row, 0)));
				}
			}
		});
		showLease.setLayout(new GridLayout(0, 2, 0, 0));

		JPanel showLeaseInfoContainer = new JPanel();
		showLease.add(showLeaseInfoContainer);

		showLeaseInfoContainer.setLayout(new BoxLayout(showLeaseInfoContainer,
				BoxLayout.Y_AXIS));

		JPanel showLeaseInfoPanel = new JPanel();
		showLeaseInfoContainer.add(showLeaseInfoPanel);
		showLeaseInfoPanel.setBorder(BorderFactory
				.createTitledBorder("Lease Info"));
		;
		showLeaseInfoPanel.setLayout(new BoxLayout(showLeaseInfoPanel,
				BoxLayout.X_AXIS));

		JPanel showLeaseLeft = new JPanel();
		showLeaseInfoPanel.add(showLeaseLeft);
		showLeaseLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel showIdLabelPanel = new JPanel();
		showIdLabelPanel.setLayout(new BoxLayout(showIdLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseLeft.add(showIdLabelPanel);

		JLabel showIdLabel = new JLabel("ID");
		showIdLabelPanel.add(showIdLabel);
		showIdLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showCustomerLabelPanel = new JPanel();
		showCustomerLabelPanel.setLayout(new BoxLayout(showCustomerLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseLeft.add(showCustomerLabelPanel);

		JLabel showCustomerLabel = new JLabel("Customer");
		showCustomerLabelPanel.add(showCustomerLabel);
		showCustomerLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showEmployeeLabelPanel = new JPanel();
		showEmployeeLabelPanel.setLayout(new BoxLayout(showEmployeeLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseLeft.add(showEmployeeLabelPanel);

		JLabel showEmployeeLabel = new JLabel("Employee");
		showEmployeeLabelPanel.add(showEmployeeLabel);
		showEmployeeLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showStatusLabelPanel = new JPanel();
		showStatusLabelPanel.setLayout(new BoxLayout(showStatusLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseLeft.add(showStatusLabelPanel);

		JLabel showStatusLabel = new JLabel("Status");
		showStatusLabelPanel.add(showStatusLabel);
		showStatusLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showPaymentTermLabelPanel = new JPanel();
		showPaymentTermLabelPanel.setLayout(new BoxLayout(
				showPaymentTermLabelPanel, BoxLayout.X_AXIS));
		showLeaseLeft.add(showPaymentTermLabelPanel);

		JLabel showPaymentTermLabel = new JLabel("Payment Term");
		showPaymentTermLabelPanel.add(showPaymentTermLabel);
		showPaymentTermLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showStartDateLabelPanel1 = new JPanel();
		showStartDateLabelPanel1.setLayout(new BoxLayout(
				showStartDateLabelPanel1, BoxLayout.X_AXIS));
		showLeaseLeft.add(showStartDateLabelPanel1);

		JLabel showStartDateLabel = new JLabel("Start Date");
		showStartDateLabelPanel1.add(showStartDateLabel);
		showStartDateLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showEndDateLabelPanel = new JPanel();
		showEndDateLabelPanel.setLayout(new BoxLayout(showEndDateLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseLeft.add(showEndDateLabelPanel);

		JLabel showEndDateLabel = new JLabel("End Date");
		showEndDateLabelPanel.add(showEndDateLabel);
		showEndDateLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showDeliveryAddressLabelPanel = new JPanel();
		showDeliveryAddressLabelPanel.setLayout(new BoxLayout(
				showDeliveryAddressLabelPanel, BoxLayout.X_AXIS));
		showLeaseLeft.add(showDeliveryAddressLabelPanel);

		JLabel showDeliveryAddressLabel = new JLabel("Delivery Address");
		showDeliveryAddressLabelPanel.add(showDeliveryAddressLabel);
		showDeliveryAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showPickupLabelPanel = new JPanel();
		showPickupLabelPanel.setLayout(new BoxLayout(showPickupLabelPanel,
				BoxLayout.X_AXIS));
		showLeaseLeft.add(showPickupLabelPanel);

		JLabel showPickupLabel = new JLabel("Self Pickup");
		showPickupLabelPanel.add(showPickupLabel);
		showPickupLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showLeaseRight = new JPanel();
		showLeaseInfoPanel.add(showLeaseRight);
		showLeaseRight
				.setLayout(new BoxLayout(showLeaseRight, BoxLayout.Y_AXIS));

		JPanel showIdFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showLeaseRight.add(showIdFieldPanel);

		showIdField = new JTextField();
		showIdFieldPanel.add(showIdField);
		showIdField.setHorizontalAlignment(SwingConstants.CENTER);
		showIdField.setEditable(false);
		showIdField.setColumns(5);

		JPanel showCustomerFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseRight.add(showCustomerFieldPanel);

		showCustomerField = new JTextField();
		showCustomerFieldPanel.add(showCustomerField);
		showCustomerField.setHorizontalAlignment(SwingConstants.CENTER);
		showCustomerField.setEditable(false);
		showCustomerField.setColumns(20);

		JPanel showEmployeeFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseRight.add(showEmployeeFieldPanel);

		showEmployeeField = new JTextField();
		showEmployeeFieldPanel.add(showEmployeeField);
		showEmployeeField.setHorizontalAlignment(SwingConstants.CENTER);
		showEmployeeField.setEditable(false);
		showEmployeeField.setColumns(25);

		JPanel showStatusFieldPanel = new JPanel(
				new FlowLayout(FlowLayout.LEFT));
		showLeaseRight.add(showStatusFieldPanel);

		showStatusField = new JTextField();
		showStatusFieldPanel.add(showStatusField);
		showStatusField.setHorizontalAlignment(SwingConstants.CENTER);
		showStatusField.setEditable(false);
		showStatusField.setColumns(20);

		JPanel showPaymentTermFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseRight.add(showPaymentTermFieldPanel);

		showPaymentTermField = new JTextField();
		showPaymentTermFieldPanel.add(showPaymentTermField);
		showPaymentTermField.setHorizontalAlignment(SwingConstants.CENTER);
		showPaymentTermField.setEditable(false);
		showPaymentTermField.setColumns(5);

		JPanel showStartDateFieldPanel1 = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseRight.add(showStartDateFieldPanel1);

		showStartDateField = new JTextField();
		showStartDateFieldPanel1.add(showStartDateField);
		showStartDateField.setHorizontalAlignment(SwingConstants.CENTER);
		showStartDateField.setEditable(false);
		showStartDateField.setColumns(15);

		JPanel showEndDateFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseRight.add(showEndDateFieldPanel);

		showEndDateField = new JTextField();
		showEndDateFieldPanel.add(showEndDateField);
		showEndDateField.setHorizontalAlignment(SwingConstants.CENTER);
		showEndDateField.setEditable(false);
		showEndDateField.setColumns(15);

		JPanel showDeliveryAddressFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showLeaseRight.add(showDeliveryAddressFieldPanel);

		showDeliveryAddressField = new JTextField();
		showDeliveryAddressFieldPanel.add(showDeliveryAddressField);
		showDeliveryAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		showDeliveryAddressField.setEditable(false);
		showDeliveryAddressField.setColumns(25);

		JPanel showPickupFieldPanel = new JPanel(
				new FlowLayout(FlowLayout.LEFT));
		showLeaseRight.add(showPickupFieldPanel);

		rdbtnYes_2 = new JRadioButton("Yes");
		rdbtnYes_2.setEnabled(false);
		showPickupFieldPanel.add(rdbtnYes_2);

		rdbtnNo_2 = new JRadioButton("No");
		rdbtnNo_2.setEnabled(false);
		showPickupFieldPanel.add(rdbtnNo_2);

		JPanel showBottomMenu = new JPanel();
		showLeaseInfoContainer.add(showBottomMenu);
		showBottomMenu.setLayout(new BorderLayout(0, 0));

		JPanel showBottomMenuRight = new JPanel();
		showBottomMenu.add(showBottomMenuRight, BorderLayout.CENTER);

		if (loggedInPosition.equals("admin")
				|| loggedInPosition.equals("salesman")) {
			JButton showBtnEdit = new JButton("Edit");
			showBtnEdit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					editLease(Integer.parseInt(showIdField.getText()));
				}
			});
			showBottomMenuRight.add(showBtnEdit);

			JButton showBtnDelete = new JButton("Delete");
			showBtnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int reply = JOptionPane.showConfirmDialog(null,
							"Are you sure you want to delete this lease ?",
							"Delete Lease", JOptionPane.YES_NO_OPTION,
							JOptionPane.WARNING_MESSAGE);
					if (reply == JOptionPane.YES_OPTION) {
						boolean success = true;
						try {
							leaseCtr.removeLease(Integer.parseInt(showIdField
									.getText()));
						} catch (NumberFormatException e1) {
							success = false;
							JOptionPane.showMessageDialog(null,
									"Field value invalid", "Error",
									JOptionPane.ERROR_MESSAGE);
						} catch (RealException e1) {
							success = false;
							JOptionPane.showMessageDialog(null, e1.getError(),
									"Error", JOptionPane.ERROR_MESSAGE);
						}
						if (success) {
							JOptionPane.showMessageDialog(null,
									"The lease was deleted", "Delete Lease",
									JOptionPane.INFORMATION_MESSAGE);
							showAllLeases();
						}
					}
				}
			});
			showBottomMenuRight.add(showBtnDelete);

		}

		JPanel showBottomMenuLeft = new JPanel();
		showBottomMenu.add(showBottomMenuLeft, BorderLayout.WEST);

		JButton showBtnBack = new JButton("Back");
		showBtnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllLeases();
			}
		});
		showBottomMenuLeft.add(showBtnBack);

		JPanel showLeaseItems = new JPanel();
		showLease.add(showLeaseItems);
		showLeaseItems.setBorder(BorderFactory
				.createTitledBorder("Lease Items"));
		showLeaseItems.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JScrollPane scrollPane_1 = new JScrollPane();
		showLeaseItems.add(scrollPane_1);

		table2 = new JTable(tableModel2);

		table2.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
		table2.setAutoCreateRowSorter(true);

		scrollPane_1.setViewportView(table2);

		showAllLeasesPanel.setLayout(new BoxLayout(showAllLeasesPanel,
				BoxLayout.X_AXIS));

		JScrollPane scrollPane = new JScrollPane();
		showAllLeasesPanel.add(scrollPane);

		scrollPane.setViewportView(table);
		showAllLeases();
	}

	protected void showLease(int id) {
		boolean success = true;
		Lease lease = null;
		try {
			lease = leaseRecords.getLease(id);
			if (lease.getStatus() != Status.BorrowedLease)
				throw new RealException("Lease is not found");
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			showIdField.setText(Integer.toString(lease.getId()));
			showCustomerField.setText(lease.getCustomer().getName());
			showEmployeeField.setText(lease.getEmployee().getName());
			showStatusField.setText(lease.getStatus().toString());
			showPaymentTermField.setText(Integer.toString(lease.getCustomer()
					.getGroup().getPaymentTerm()));
			showStartDateField.setText(lease.getStartDateFormatted());
			showEndDateField.setText(lease.getEndDateFormatted());

			showDeliveryAddressField.setText(lease.getdAddress());
			if (lease.getPickup()) {
				rdbtnYes_2.setSelected(true);
				rdbtnNo_2.setSelected(false);
			} else {
				rdbtnYes_2.setSelected(false);
				rdbtnNo_2.setSelected(true);
			}

			generateLeaseTabel(lease.getId(), tableModel2);

			CardLayout cl = (CardLayout) (leaseContainer.getLayout());
			cl.show(leaseContainer, "showLease");
		}
	}

	protected void editLease(int id) {
		Lease lease = null;
		boolean success = true;
		try {
			lease = leaseRecords.getLease(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			editIdField.setText(Integer.toString(lease.getId()));
			editCustomerField.setText(Integer.toString(lease.getCustomer()
					.getId()));
			editEmployeeField.setText(Integer.toString(lease.getEmployee()
					.getId()));
			editStatusField.setText(lease.getStatus().toString());
			editPaymentTermField.setText(Integer.toString(lease.getCustomer()
					.getGroup().getPaymentTerm()));
			editStartDateField.setText(lease.getStartDateFormatted());
			editEndDateField.setText(lease.getEndDateFormatted());
			editDeliveryAddressField.setText(lease.getdAddress());

			if (lease.getPickup()) {
				rdbtnYes.setSelected(true);
				rdbtnNo.setSelected(false);
				editDeliveryAddressField.setEditable(false);
			} else {
				rdbtnYes.setSelected(false);
				rdbtnNo.setSelected(true);
				editDeliveryAddressField.setEditable(true);
			}

			generateLeaseTabel(id, tableModel3);

			CardLayout cl = (CardLayout) (leaseContainer.getLayout());
			cl.show(leaseContainer, "editLease");
		}
	}

	public class AddLeaseItemUnitPopupWindow extends JFrame {
		private static final long serialVersionUID = 1L;
		private JTextField textField_leaseItem;
		private JTextField textField_serialNo;

		public AddLeaseItemUnitPopupWindow(final int leaseId) {
			super("Add Lease item unit");
			setSize(450, 100);
			setLocationRelativeTo(null);
			setVisible(true);
			JLabel productBarcode = new JLabel("Lease Item id:");
			JLabel productQuantity = new JLabel("Unit serial number:");
			textField_leaseItem = new JTextField();
			textField_leaseItem.setColumns(8);
			textField_serialNo = new JTextField();
			textField_serialNo.setColumns(8);
			JButton btnAddSalelineOK = new JButton("OK");
			btnAddSalelineOK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean success = true;
					try {
						int leaseItemId = Integer.parseInt(textField_leaseItem
								.getText());
						int serialNo = Integer.parseInt(textField_serialNo
								.getText());
						leaseCtr.addLeaseItemUnit(leaseId, leaseItemId,
								serialNo);
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, "Field invalid.",
								"Error", JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						setVisible(false);
						generateLeaseTabel(leaseId, tableModel3);
					}
				}
			});
			JButton btnAddSalelineCancel = new JButton("Cancel");
			btnAddSalelineCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
				}
			});

			JPanel addSalelinePanel = new JPanel();
			addSalelinePanel.setLayout(new BoxLayout(addSalelinePanel,
					BoxLayout.Y_AXIS));
			JPanel addSalelineInfoPanel = new JPanel();
			addSalelineInfoPanel.setLayout(new FlowLayout());
			JPanel addSalelineChoicePanel = new JPanel();
			addSalelineChoicePanel.setLayout(new FlowLayout());

			addSalelinePanel.add(addSalelineInfoPanel);
			addSalelinePanel.add(addSalelineChoicePanel);
			addSalelineInfoPanel.add(productBarcode);
			addSalelineInfoPanel.add(textField_leaseItem);
			addSalelineInfoPanel.add(productQuantity);
			addSalelineInfoPanel.add(textField_serialNo);
			addSalelineChoicePanel.add(btnAddSalelineOK);
			addSalelineChoicePanel.add(btnAddSalelineCancel);

			add(addSalelinePanel);

		}
	}

	public class RemoveLeaseItemUnitPopupWindow extends JFrame {
		private static final long serialVersionUID = 1L;
		private JTextField textField_leaseItem;
		private JTextField textField_serialNo;

		public RemoveLeaseItemUnitPopupWindow(final int leaseId) {
			super("Remove Lease item unit");
			setSize(450, 100);
			setLocationRelativeTo(null);
			setVisible(true);
			JLabel productBarcode = new JLabel("Lease Item id:");
			JLabel productQuantity = new JLabel("Unit serial number:");
			textField_leaseItem = new JTextField();
			textField_leaseItem.setColumns(8);
			textField_serialNo = new JTextField();
			textField_serialNo.setColumns(8);
			JButton btnAddSalelineOK = new JButton("OK");
			btnAddSalelineOK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					boolean success = true;
					try {
						int leaseItemId = Integer.parseInt(textField_leaseItem
								.getText());
						int serialNo = Integer.parseInt(textField_serialNo
								.getText());
						leaseCtr.removeLeaseItemUnit(leaseId, leaseItemId,
								serialNo);
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, "Field invalid.",
								"Error", JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						setVisible(false);
						generateLeaseTabel(leaseId, tableModel3);
					}
				}
			});
			JButton btnAddSalelineCancel = new JButton("Cancel");
			btnAddSalelineCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
				}
			});

			JPanel addSalelinePanel = new JPanel();
			addSalelinePanel.setLayout(new BoxLayout(addSalelinePanel,
					BoxLayout.Y_AXIS));
			JPanel addSalelineInfoPanel = new JPanel();
			addSalelineInfoPanel.setLayout(new FlowLayout());
			JPanel addSalelineChoicePanel = new JPanel();
			addSalelineChoicePanel.setLayout(new FlowLayout());

			addSalelinePanel.add(addSalelineInfoPanel);
			addSalelinePanel.add(addSalelineChoicePanel);
			addSalelineInfoPanel.add(productBarcode);
			addSalelineInfoPanel.add(textField_leaseItem);
			addSalelineInfoPanel.add(productQuantity);
			addSalelineInfoPanel.add(textField_serialNo);
			addSalelineChoicePanel.add(btnAddSalelineOK);
			addSalelineChoicePanel.add(btnAddSalelineCancel);

			add(addSalelinePanel);

		}
	}

	protected void showAllLeases() {
		ArrayList<Lease> leases = leaseRecords.getLeases();
		tableModel.setRowCount(0);
		for (Lease lease : leases) {
			if (lease.getStatus() == Status.BorrowedLease) {
				tableModel.addRow(new Object[] {
						lease.getId(),
						lease.getCustomer().getName(),
						lease.getCustomer().getGroup().getId(),
						lease.getStartDateFormatted(),
						lease.getEndDateFormatted(),
						Double.toString(Double
								.valueOf(new DecimalFormat("#.##").format(lease
										.calculateFinalPrice()))) });
			}
		}
		CardLayout cl_leaseContainer = (CardLayout) (leaseContainer.getLayout());
		cl_leaseContainer.show(leaseContainer, "showAllLeases");
	}

	public long stringToLongDate(String input) throws ParseException,
			RealException {
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		formatter.setLenient(false);
		java.util.Date utilDate = null;
		utilDate = formatter.parse(input);
		if (!formatter.format(utilDate).equals(input))
			throw new RealException("Field value invalid");
		return utilDate.getTime();
	}

	public void generateLeaseTabel(int leaseId, DefaultTableModel theTableModel) {
		ArrayList<LeaseItemUnit> leaseItemUnits = null;
		try {
			leaseItemUnits = leaseCtr.getlease(leaseId).getLeaseItemUnits();
		} catch (RealException e) {
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		theTableModel.setRowCount(0);
		for (LeaseItemUnit leaseUnit : leaseItemUnits) {
			theTableModel
					.addRow(new Object[] { leaseUnit.getLeaseItem().getId(),
							leaseUnit.getLeaseItem().getName(),
							leaseUnit.getLeaseItem().getLocation(),
							leaseUnit.getSerialNo(),
							leaseUnit.getStatus().toString() });
		}
		try {
			double totalPrice = leaseCtr.getlease(leaseId)
					.calculateFinalPrice();
			String[] space = { "", "", "", "", "" };
			String[] totalPriceRow = {
					"",
					"",
					"",
					"Total Price:",
					Double.toString(Double.valueOf(new DecimalFormat("#.##")
							.format(totalPrice))) };
			theTableModel.addRow(space);
			theTableModel.addRow(space);
			theTableModel.addRow(totalPriceRow);
		} catch (RealException e) {
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public Long getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		java.util.Date utilDate = cal.getTime();
		return utilDate.getTime();
	}

	public String getCurrentDateAsString() {
		Calendar cal = Calendar.getInstance();
		java.util.Date utilDate = cal.getTime();
		String orderDate = new SimpleDateFormat("dd/MM/yyyy").format(utilDate);
		return orderDate;
	}

}
